	


version 15
capture log close
set more off
clear
clear matrix
clear mata

if c(username)=="WB485280" {
		glo rootdir		"C:\Users\wb485280\OneDrive - WBG\radicalization"
		}
if c(username)=="wb382635" {
		glo rootdir		"C:\Users\wb382635\Dropbox\Unemp & daesh"
		}
if c(username)=="WB452275" {
		glo rootdir		"C:\Users\WB452275\Dropbox\Projects\Unemp & daesh"
		}
if c(username)=="sarurchaudhary" {
		glo rootdir		"/Users/sarurchaudhary/Dropbox/Unemp & daesh"
		}
if c(username)=="kartikabhatia" {
			glo rootdir		"/Users/kartikabhatia/Dropbox/Before2019/Unemp & daesh"
			}
			
		glo	datadir     "${rootdir}/Data/Raw data"
		glo outdir		"${rootdir}/Data/Working datasets"
		glo dodir		"${rootdir}/Dofiles"
        
			
			cd "${outdir}"
			
* ------------------------------------------------------------------------------

* Date : March 2021 [Checked Oct 2021]

* Project : Daesh FF Working Paper (The World Bank)

* This is the do file for Figure 1 in the main paper

* ------------------------------------------------------------------------------
				
	 
*****************************************************************
*Figure 1: Use FWL theorem to plot the  
*Relative supply of Daesh recruits against the Relative Unemployment Rate
******************************************************************   
     	
	use "${outdir}/finaldata_CE.dta", clear 
		
		data_preamble //Invoking data prep program to clean data and create variables	
		
		//Altering variable labels
		la var unemp_logdistance             "\textbf{Interaction between unemployment and}\\Distance to Syria (log)"
		
		//Define FWL terms
		local X2=`"log_ilo2_pop educ2 educ3 ctrydummy*"'	 
		local X1=`"ilo2_unemp_educ"'
		local Y=`"logn_educ"'	

		
forvalues i=1(1)4 {
	preserve		
		//keep only one quartile and list countries
		keep if quart_dist`i'==1
		ta ctry
		
		//replicate main result for each quartile
		xtreg logn_educ ilo2_unemp_educ log_ilo2_pop educ2 educ3, fe vce(cluster ctry)
		reg   `Y' `X1' `X2' //make sure we get the same result in a simple regression with country dummies
		keep if e(sample)==1 //make sure the estimation sample is used when partialling out
		
		//apply FWL procedure
		reg   `Y'	   `X2'
		predict resY, residuals
		
		reg       `X1' `X2'
		predict resX1, residuals
		
		reg resY resX1 //the same coefficient as above should obtain
		la var resX1                "Residual Unemployment Rate (%)"	
		
		//get slope and std error
		gen sample = e(sample)
		mat SL=e(b)
		mat ST=e(V)
		matlist SL
		matlist ST
		global slope=int(1000*SL[1,1])/1000
		global stderr=int(1000*sqrt(ST[1,1]))/1000
	
		//Compute location for the slope label
		su resY if sample
		glo placey=r(min)+0.85*(r(max)-r(min))
		glo placey2=r(min)+0.75*(r(max)-r(min))
		su resX1 if sample
		glo placex=r(min)+0.15*(r(max)-r(min))
		
		//Create figure
		local ytitle "Relative supply of Deash recruits (log)"
		local xtitle "Relative unemployment rate"
		glo label3=`"label(3 "Tertiary")"'
		glo label2=`"label(2 "Secondary")"'
		glo label1=`"label(1 "Primary")"'		
		twoway 	(scatter 	resY resX1 if education_level==1 & sample, 	///
				 mcolor(black )  msymbol(circle) mlabel(countrycode) mlabcolor(black)  mlabsize(vsmall)) || ///
				(scatter 	resY resX1 if education_level==2 & sample, 	///
				 mcolor(gs8 )  msymbol(square) mlabel(countrycode)  mlabcolor(gs8)  mlabsize(vsmall)) ||  ///
				(scatter 	resY resX1 if education_level==3 & sample, 	///
				mcolor(gs4 )  msymbol(triangle) mlabel(countrycode) mlabcolor(gs4) mlabsize(vsmall)) ||  ///
				(lfit 		resY resX1, lcolor(black)) , ///
		legend($label1 $label2 $label3 row(1)) graphregion(color(white)) bgcolor(white) ///
		xtitle("`xtitle'") xlabel(-5(5)5) ytitle("`ytitle'") /// 
		text( $placey $placex "slope=$slope""             ($stderr)")
		
		graph export "resnce_res_uce_quart`i'_1.pdf", replace
	
	restore
}
	*Quartile 2
	preserve
		
		reg logn_educ log_ilo2_pop educ2 educ3 ctrydummy*  ///
		unemp_quart_dist1 unemp_quart_dist3 unemp_quart_dist4 
		predict resY, residuals

		reg unemp_quart_dist2 log_ilo2_pop educ2 educ3 ctrydummy*  ///
		unemp_quart_dist1 unemp_quart_dist3 unemp_quart_dist4  
		predict resX1, residuals
		la var resX1                "Residual Unemployment Rate (%)"
		
		keep if quart_dist==2
		
		reg resY resX1
		gen sample = e(sample)
		mat SL=e(b)
		mat ST=e(V)
		matlist SL
		matlist ST
		global slope=int(1000*SL[1,1])/1000
		global stderr=int(1000*sqrt(ST[1,1]))/1000
		
		corr resY resX1
		global rho=int(1000*`r(rho)')/1000
		
		su resY if sample
		glo placey=r(min)+0.85*(r(max)-r(min))
		glo placey2=r(min)+0.75*(r(max)-r(min))
		su resX1 if sample
		glo placex=r(min)+0.15*(r(max)-r(min))
		
		local ytitle "Relative supply of Deash recruits (log)"
		local xtitle "Relative unemployment rate"
		

		twoway 	(scatter 	resY resX1 if education_level==1 & sample, mcolor(black ) 	msymbol(circle) mlabel(countrycode)  mlabcolor(black)  mlabsize(vsmall)) || ///
		(scatter 	resY resX1 if education_level==2 & sample,  mcolor(gs8 ) 	msymbol(square) mlabel(countrycode) mlabcolor(gs8)  mlabsize(vsmall)) ||  ///
		(scatter 	resY resX1 if education_level==3 & sample,  mcolor(gs4 )  	msymbol(triangle) mlabel(countrycode)  mlabcolor(gs4)  mlabsize(vsmall)) ||  ///
		(lfit 		resY resX1, lcolor(black)) , ///
		legend($label1 $label2 $label3 row(1)) graphregion(color(white)) bgcolor(white) ///
		xtitle("`xtitle'") xlabel(-10(5)5) ytitle("`ytitle'") /// // title("`title'")
		text( $placey $placex "slope=$slope""             ($stderr)")
		
		graph export "resnce_res_uce_quart2_1.pdf", replace
	
	restore

	*Quartile 3
	preserve
		
		reg logn_educ log_ilo2_pop educ2 educ3 ctrydummy*  ///
		unemp_quart_dist1 unemp_quart_dist2 unemp_quart_dist4 
		predict resY, residuals

		reg unemp_quart_dist3 log_ilo2_pop educ2 educ3 ctrydummy*  ///
		unemp_quart_dist1 unemp_quart_dist2 unemp_quart_dist4  
		predict resX1, residuals
		la var resX1                "Residual Unemployment Rate (%)"
		
		keep if quart_dist==3
		
		reg resY resX1
		gen sample = e(sample)
		mat SL=e(b)
		mat ST=e(V)
		matlist SL
		matlist ST
		global slope=int(1000*SL[1,1])/1000
		global stderr=int(1000*sqrt(ST[1,1]))/1000
		
		corr resY resX1
		global rho=int(1000*`r(rho)')/1000
		
		su resY if sample
		glo placey=r(min)+0.85*(r(max)-r(min))
		glo placey2=r(min)+0.75*(r(max)-r(min))
		su resX1 if sample
		glo placex=r(min)+0.15*(r(max)-r(min))
		
		local ytitle "Relative supply of Deash recruits (log)"
		local xtitle "Relative unemployment rate"
		

		twoway 	(scatter 	resY resX1 if education_level==1 & sample, mcolor(black ) 	msymbol(circle) mlabel(countrycode) mlabcolor(black) mlabsize(vsmall)) || ///
		(scatter 	resY resX1 if education_level==2 & sample, mcolor(gs8 ) 	msymbol(square) mlabel(countrycode) mlabcolor(gs8) mlabsize(vsmall)) ||  ///
		(scatter 	resY resX1 if education_level==3 & sample, mcolor(gs4 ) 	msymbol(triangle) mlabel(countrycode) mlabcolor(gs4) mlabsize(vsmall)) ||  ///
		(lfit 		resY resX1, lcolor(black)) , ///
		legend($label1 $label2 $label3 row(1)) graphregion(color(white)) bgcolor(white) ///
		xtitle("`xtitle'") xlabel(-10(5)5) ytitle("`ytitle'") /// // title("`title'")
		text( $placey $placex "slope=$slope""             ($stderr)")
		
		graph export "resnce_res_uce_quart3_1.pdf", replace
	
	restore

	*Quartile 4
	preserve
		
		reg logn_educ log_ilo2_pop educ2 educ3 ctrydummy*  ///
		unemp_quart_dist1 unemp_quart_dist2 unemp_quart_dist3 
		predict resY, residuals

		reg unemp_quart_dist4 log_ilo2_pop educ2 educ3 ctrydummy*  ///
		unemp_quart_dist1 unemp_quart_dist2 unemp_quart_dist3  
		predict resX1, residuals
		la var resX1                "Residual Unemployment Rate (%)"
		
		keep if quart_dist==4
		
		reg resY resX1
		gen sample = e(sample)
		mat SL=e(b)
		mat ST=e(V)
		matlist SL
		matlist ST
		global slope=int(1000*SL[1,1])/1000
		global stderr=int(1000*sqrt(ST[1,1]))/1000
		
		corr resY resX1
		global rho=int(1000*`r(rho)')/1000
		
		su resY if sample
		glo placey=r(min)+0.85*(r(max)-r(min))
		glo placey2=r(min)+0.75*(r(max)-r(min))
		su resX1 if sample
		glo placex=r(min)+0.15*(r(max)-r(min))
		
		local ytitle "Relative supply of Deash recruits (log)"
		local xtitle "Relative unemployment rate"
		

		twoway 	(scatter 	resY resX1 if education_level==1 & sample, mcolor(black ) 	msymbol(circle) mlabel(countrycode) mlabcolor(black) mlabsize(vsmall)) || ///
		(scatter 	resY resX1 if education_level==2 & sample, mcolor(gs8 ) 	msymbol(square) mlabel(countrycode) mlabcolor(gs8) mlabsize(vsmall)) ||  ///
		(scatter 	resY resX1 if education_level==3 & sample, mcolor(gs4 ) 	msymbol(triangle) mlabel(countrycode) mlabcolor(gs4) mlabsize(vsmall)) ||  ///
		(lfit 		resY resX1, lcolor(black)) , ///
		legend($label1 $label2 $label3 row(1)) graphregion(color(white)) bgcolor(white) ///
		xtitle("`xtitle'") xlabel(-10(5)5) ytitle("`ytitle'") /// // title("`title'")
		text( $placey $placex "slope=$slope""             ($stderr)")
		
		graph export "resnce_res_uce_quart4_1.pdf", replace
	
	restore
	
